<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

import { getDataCount } from '@/api/system/statement'

const animationDuration = 3000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      // 查询条件
      getBusinessdataForm: {
        statementKey: undefined
      },
      getMessageForm: {
        statementKey: undefined
      },
      getApplyForm: {
        statementKey: undefined
      },

      radarindicator: [
        { name: 'Mon', max: 200 },
        { name: 'Tue', max: 500 },
        { name: 'Wed', max: 500 },
        { name: 'Thu', max: 500 },
        { name: 'Fri', max: 500 },
        { name: 'Sat', max: 500 }
      ],
      radardata: [
        {
          value: [5000, 7000, 12000, 11000, 15000, 14000],
          name: '业务量'
        },
        {
          value: [4000, 9000, 15000, 15000, 13000, 11000],
          name: '信息量'
        },
        {
          value: [5500, 11000, 12000, 15000, 12000, 12000],
          name: '数据量'
        }
      ]
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },

  created() {
    this.getBusiDataCount()
    this.getMessageDataCount()
    this.getApplyDataCount()
  },

  methods: {
    // 获取雷达图最大值
    getECharMax() {
      const dataMap = this.radardata.map(item => item.value)
      // console.log('============', dataMap)

      for (let index = 0; index < 6; index++) {
        const element1 = dataMap[0][index]
        // console.log('element1 ------------', element1)

        const element2 = dataMap[1][index]
        // console.log('element2 ------------', element2)

        const element3 = dataMap[2][index]
        // console.log('element3 ------------', element3)

        this.getMax(element1, element2, element3, index)
      }
    },

    // 获取并设定雷达图最大值
    getMax(v1, v2, v3, index) {
      const array = []
      array.push(v1)
      array.push(v2)
      array.push(v3)

      // console.log('arr ---------- ', array)

      for (let index = 0; index < array.length; index++) {
        for (let j = 0; j < array.length - 1; j++) {
          if (array[j] < array[j + 1]) {
            const temp = array[j + 1]
            array[j + 1] = array[j]
            array[j] = temp
          }
        }
      }

      // console.log('排序过后值 ======= ', array)
      // console.log('----------', array[0])
      // console.log('----------', this.radarindicator[index].max)

      this.radarindicator[index].max = array[0] + 100
    },

    // 获取业务数据
    getBusiDataCount() {
      // console.log('radardata before ============', this.radardata[0].value)
      this.getBusinessdataForm.statementKey = 'approveRadar'
      getDataCount(this.getBusinessdataForm).then(res => {
        const dataCountArr = res.data.map(item => item.dataCount)
        this.radardata[0].value = dataCountArr
        // console.log('radardata after ============', this.radardata[0].value)

        this.getECharMax()
        this.initChart()
      })
    },

    // 获取信息量
    getMessageDataCount() {
      // console.log('radardata before ============', this.radardata[1].value)
      this.getMessageForm.statementKey = 'noticeRadar'
      getDataCount(this.getMessageForm).then(res => {
        const dataCountArr = res.data.map(item => item.dataCount)
        this.radardata[1].value = dataCountArr
        // console.log('radardata after ============', this.radardata[1].value)
        this.getECharMax()
        this.initChart()
      })
    },

    // 获取数据量
    getApplyDataCount() {
      // console.log('radardata before ============', this.radardata[2].value)
      this.getApplyForm.statementKey = 'applyRadar'
      getDataCount(this.getApplyForm).then(res => {
        const dataCountArr = res.data.map(item => item.dataCount)
        this.radardata[2].value = dataCountArr
        // console.log('radardata after ============', this.radardata[2].value)
        this.getECharMax()
        this.initChart()
      })
    },

    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        radar: {
          radius: '66%',
          center: ['50%', '42%'],
          splitNumber: 8,
          splitArea: {
            areaStyle: {
              color: 'rgba(127,95,132,.3)',
              opacity: 1,
              shadowBlur: 45,
              shadowColor: 'rgba(0,0,0,.5)',
              shadowOffsetX: 0,
              shadowOffsetY: 15
            }
          },
          indicator: this.radarindicator
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['业务量', '信息量', '数据量']
        },
        series: [{
          type: 'radar',
          symbolSize: 0,
          areaStyle: {
            normal: {
              shadowBlur: 13,
              shadowColor: 'rgba(0,0,0,.2)',
              shadowOffsetX: 0,
              shadowOffsetY: 10,
              opacity: 1
            }
          },
          data: this.radardata,
          animationDuration: animationDuration
        }]
      })
    }
  }
}
</script>
